$(function() {
	//	登录
	layui.use('form', function() {
		var form = layui.form;
		//监听提交
		form.on('submit(LAY-user-login-submit)', function(data) {
			console.log(data.field)
			ajaxPost({
				url: 'user/login',
				data: {
					userName: data.field.username,
					password: data.field.password
				},
				successFull: function(res) {
					console.log(res)
					layer.msg('登录成功', {
						icon: 6
					});
					var user = {
						menuList: res.menuList,
						msg: res.msg
					}
					window.sessionStorage.setItem("user", JSON.stringify(user))
					window.location.href = "../welcome/welcome.html?pos=6";
				},
				errCallBack: function(res) {
					layer.msg(res.msg, {
						icon: 6
					});
				}
			})
		});
		//		找回密码提交
		form.on('submit(LAY-user-forget-submit)', function(data) {
			console.log(data.field)
			ajaxPost({
				url: 'user/updatePwd',
				data: {
					userPwd: data.field.password,
					userAccount: data.field.username
				},
				successFull: function(res) {
					layer.msg('密码修改成功', {
						icon: 6
					});
					setTimeout(function() {
						window.location.href = "login.html";
					}, 2000);
				},
				errCallBack: function(res) {
					if(res.code == 201) {
						layer.msg('不存在此用户', {
							icon: 6
						});
					}
				}
			})
		});
	});

	//	获取验证码
	var flag = true
	$('#vscode').on('click', function(e) {
		$("input").blur();
		console.log(flag);
		if(flag) {
			if(yanPhone($("#LAY-user-login-username").val())) {
				intalvalNum();
				flag = false;
				ajaxGet({
					url: '',
					data: {
						phone: yanPhone($("#LAY-user-login-username").val())
					},
					successFull: function(res) {
						console.log(res);
					},
					errCallBack: function(res) {
						clearInterval(timer);
						$(".vscode").html('获取验证码');
						flag = true;
						layer.msg('获取验证码失败', {
							icon: 6
						});
					}
				})

			};
		}
	})

	//  短信验证码倒计时
	function intalvalNum() {
		var num = 60;
		timer = setInterval(function() {
			if(num <= 0) {
				$(".validateBth").html('获取验证码');
				clearInterval(timer);
				flag = true;
				num = 180;
			} else {
				num--;
				$(".validateBth").html(num);
			}
		}, 1000);
	}
	//登录校验手机号
	function yanPhone(n) {
		var reg = /^[1][3,4,5,7,8][0-9]{9}$/;
		var phone;
		if(!n || n.length != 11) {
			layer.msg('请输入正确的手机号', {
				icon: 6
			});
			phone = false;
			return false;
		}
		if(reg.test(n)) {
			phone = n;
		} else {
			layer.msg('手机号格式错误', {
				icon: 6
			});
			phone = false;
			return false;
		}
		return phone
	}

})